#include <gtest/gtest.h>
#include <fstream>
#include "rtdev/safetyboard/scbitf.h"

using rtdev::SafetyBoardItf;

class RoboHubConfigFile : public ::testing::Test
{
public:
    virtual void SetUp() override {}
    virtual void TearDown() override { std::remove(cfgfile.c_str()); }
    void load_config_sim()
    {
        std::string cfg = R"(
<?xml version="1.0" encoding="UTF-8"?>
<cab>
    <info name="RoboHub" type="4" sim="1"/>
    <bus chn="6" chn2="7" desc="dualcan"/>
</cab>
        )";
        std::ofstream ofs(cfgfile);
        ofs << cfg;
        ofs.close();
    }
    void load_config_real()
    {
        std::string cfg = R"(
<?xml version="1.0" encoding="UTF-8"?>
<cab>
    <info name="RoboHub" type="4" sim="0"/>
    <bus chn="6" chn2="7" desc="dualcan"/>
</cab>
        )";
        std::ofstream ofs(cfgfile);
        ofs << cfg;
        ofs.close();
    }
    std::string cfgfile = "/tmp/RoboHub.xml";
};

TEST_F(RoboHubConfigFile, create_sim)
{
    auto pscb = SafetyBoardItf::create(rtdev::kScbTypeRoboHub);
    EXPECT_TRUE(pscb != nullptr);
    load_config_sim();
    ASSERT_EQ(pscb->init(cfgfile.c_str()), 0);
    ASSERT_EQ(pscb->is_sim(), true);
    EXPECT_EQ(pscb->cab()->get_hw_ver(), 0xffffff);
    EXPECT_EQ(pscb->cab()->get_sw_ver(), 0xffffff);
    EXPECT_STREQ(pscb->cab()->get_version_str().c_str(), "RoboHub HW:0.0.0S SW:0.0.0S");
    pscb->get_recv_data();
    pscb->set_send_data();
    EXPECT_TRUE(pscb->is_alive());
}

TEST_F(RoboHubConfigFile, create_real)
{
    auto pscb = SafetyBoardItf::create(rtdev::kScbTypeRoboHub);
    EXPECT_TRUE(pscb != nullptr);
    load_config_real();
    ASSERT_EQ(pscb->init(cfgfile.c_str()), 0);
    ASSERT_EQ(pscb->is_sim(), false);
    EXPECT_EQ(pscb->cab()->get_hw_ver(), 0);
    EXPECT_EQ(pscb->cab()->get_sw_ver(), 0);
    EXPECT_STREQ(pscb->name(), "RoboHub");
    EXPECT_STREQ(pscb->cab()->get_version_str().c_str(), "RoboHub HW:0.0.0 SW:0.0.0");
    pscb->get_recv_data();
    pscb->set_send_data();
    EXPECT_FALSE(pscb->is_alive());
}

TEST_F(RoboHubConfigFile, create_dual_canbus) {}